<?php
namespace app\controller\admin\info;

use app\BaseController;
use app\common\repositories\member\MemberRecordRepository;
use app\common\repositories\member\MemberRepository as repository;
use think\App;
use think\exception\ValidateException;

class Member extends BaseController
{
    protected $repository;

    public function __construct(App $app, repository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }


    // 会员开通记录
    public function memberRecord()
    {
        [$page,$limit] = $this->request->page();

        $count = app()->make(MemberRecordRepository::class)->getSearch()
            ->where(['status'=>1])->count();
        $list = app()->make(MemberRecordRepository::class)->getSearch()
            ->where(['status'=>1])->page($page,$limit)->select();

        return success(compact('count','list'));
    }


    public function lst()
    {
        $level = input('level');
        $list = $this->repository->getSearch()->where('is_del',0)->where('level',$level)->order('type asc,price asc')->select();
        return success($list);
    }


    public function create()
    {
        $data = $this->checkParams();
        $this->repository->create($data);
        return success('添加成功');
    }


    public function update($id)
    {
        $content = $this->repository->get($id);
        if(!$content)
            throw new ValidateException('数据不存在');
        $data = $this->checkParams();
        $content->save($data);
        return success('修改成功');
    }


    public function checkParams()
    {
        $params = $this->request->params(['title','type','price','count','level']);
        validate([
            'title|套餐名称'=>'require|max:250',
            'type|套餐类型'=>'require',
            'price|套餐价格'=>'require',
            'level|代理等级'=>'require'
        ])->check($params);

        if($params['type'] == 1){
            if(!$params['count'])
                throw new ValidateException('请输入线索数量');
        }

        return $params;
    }


    public function delete($id)
    {
        $member = $this->repository->get($id);
        if($member){
            $member->save(['is_del'=>1]);
        }
        return success('删除成功');
    }

}